package haoyou.csnf.manager.module.pm.service.employeetitles;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.employeetitles.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeetitles.EmployeeTitlesDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.employeetitles.EmployeeTitlesMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 职工职称 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class EmployeeTitlesServiceImpl implements EmployeeTitlesService {

    @Resource
    private EmployeeTitlesMapper employeeTitlesMapper;

    @Override
    public Long createEmployeeTitles(EmployeeTitlesSaveReqVO createReqVO) {
        // 插入
        EmployeeTitlesDO employeeTitles = BeanUtils.toBean(createReqVO, EmployeeTitlesDO.class);
        employeeTitlesMapper.insert(employeeTitles);
        // 返回
        return employeeTitles.getId();
    }

    @Override
    public void updateEmployeeTitles(EmployeeTitlesSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeTitlesExists(updateReqVO.getId());
        // 更新
        EmployeeTitlesDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeTitlesDO.class);
        employeeTitlesMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmployeeTitles(Long id) {
        // 校验存在
        validateEmployeeTitlesExists(id);
        // 删除
        employeeTitlesMapper.deleteById(id);
    }

    @Override
        public void deleteEmployeeTitlesListByIds(List<Long> ids) {
        // 校验存在
        validateEmployeeTitlesExists(ids);
        // 删除
        employeeTitlesMapper.deleteByIds(ids);
        }

    private void validateEmployeeTitlesExists(List<Long> ids) {
        List<EmployeeTitlesDO> list = employeeTitlesMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(EMPLOYEE_TITLES_NOT_EXISTS);
        }
    }

    private void validateEmployeeTitlesExists(Long id) {
        if (employeeTitlesMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_TITLES_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeTitlesDO getEmployeeTitles(Long id) {
        return employeeTitlesMapper.selectById(id);
    }

    @Override
    public PageResult<EmployeeTitlesDO> getEmployeeTitlesPage(EmployeeTitlesPageReqVO pageReqVO) {
        return employeeTitlesMapper.selectPage(pageReqVO);
    }

}